/*
 * User: rant
 * Date: Aug 6, 2007
 *
 *
 */
package laMalinche.testingPlayGround.testModel;

import laMalinche.rete.nodes.tailing.AccessReporter;
import laMalinche.propertyChangeAdapters.model.PropertyChangeObservable;
import laMalinche.propertyChangeAdapters.model.OgnlHolder;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

/**
 *
 */
public class
		PersonVo implements PropertyChangeObservable, OgnlHolder {
private AccessReporter accessReporter;

	String ognl;

	public PersonVo() {
		setOgnl("");
	}

	public String getOgnl() {
		return ognl;
	}

	public void setOgnl(final String ognl) {
		this.ognl = ognl;
	}

	public AccessReporter getAccessReporter() {
		return accessReporter;
	}

	public void setAccessReporter(final PropertyChangeListener accessReporter) {
					this.accessReporter = (AccessReporter) accessReporter;
					accessSupport.addPropertyChangeListener( accessReporter);
	}

	private String name;
	private String description;
	private Integer age;
	private PersonVo father;
	private PersonVo mother;
	private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
	private PropertyChangeSupport accessSupport = new PropertyChangeSupport(this);

	public PersonVo getFather() {
		return father;
	}

	private void fireAccessReport(final String propertyName) {
		accessSupport.firePropertyChange(propertyName, "1", "");
	}

	public void setFather(final PersonVo father) {
		father.setAccessReporter((PropertyChangeListener) getAccessReporter());
		father.setOgnl(getOgnl()+".father");
		this.father = father;
	}

	public PersonVo getMother() {
			return mother;
	}

	public void setMother(final PersonVo mother) {
		mother.setAccessReporter((PropertyChangeListener) getAccessReporter());
		mother.setOgnl(getOgnl()+".mother");
		this.mother = mother;

	}

	public String getName() {
		fireAccessReport("name");
		return name;
	}

	public void setName(String name) {
		String oldName = this.name;
		this.name = name;
		firePropertyChange("name", oldName, name);
	}

	public Integer getAge() {
		fireAccessReport("age");
		return age;
	}

	public void setAge(Integer age) {
		Integer oldValue =this.age;
		this.age = age;
		firePropertyChange("age", oldValue, age);
	}

	public String getDescription() {
		fireAccessReport("description");
		return description;
	}

	public void setDescription(String description) {
		String oldDescription = this.description;
		this.description = description;
		firePropertyChange("description", oldDescription, description);
	}

	private void firePropertyChange(String propertyName,
									Object oldValue,
									Object newValue) {
//		if (oldValue == null && newValue != null ||
//				!(oldValue==newValue)) {
        pcs.firePropertyChange(propertyName, oldValue, newValue);
//		}
	}

	public void addPropertyChangeListener(PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(listener);
	}

	public void addPropertyChangeListener(String propertyName,
										  PropertyChangeListener listener) {
		pcs.addPropertyChangeListener(propertyName, listener);
	}


	public void removeAccessSuportListener(   PropertyChangeListener listener) {
		accessSupport.removePropertyChangeListener( listener);
	}

	public void removePropertyChangeListener(PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(listener);
	}

	public void removePropertyChangeListener(String propertyName,
											 PropertyChangeListener listener) {
		pcs.removePropertyChangeListener(propertyName, listener);
	}

	public void detach() {
		accessSupport = null;
		if (getFather() != null) {
            getFather().detach();
        }
		if (getMother() != null) {
            getMother().detach();
        }
	}
}

